using Microsoft.EntityFrameworkCore;

using ELportal.Common.Models;

namespace ELportal.Repositories;

public class UserRepository : DbContext, IUserRepository
{
	private DbSet<User> Users { get; set; }

	public UserRepository(DbContextOptions<UserRepository> options) : base(options) { }

	public async Task<IReadOnlyCollection<IUser>> GetAsync(CancellationToken cancellationToken = default)
	{
		var users = await Users
			.Where(u => !u.Fired && !string.IsNullOrWhiteSpace(u.Department))
			.ToArrayAsync(cancellationToken);

		return users;
	}

	public async Task<IUser?> GetAsync(uint id, CancellationToken cancellationToken = default)
	{
		var user = await Users
			.Where(u => !u.Fired && !string.IsNullOrWhiteSpace(u.Department))
			.SingleOrDefaultAsync(x => x.Id == id, cancellationToken);

		return user;
	}

	public async Task<IUser?> GetAsync(string ldapLogin, CancellationToken cancellationToken = default)
	{
		var user = await Users
			.SingleOrDefaultAsync(x => ldapLogin.Equals(x.LdapLogin, StringComparison.OrdinalIgnoreCase), cancellationToken);

		return user;
	}
}
